;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.TurnoverRateAnalysis", [])
        .controller("TurnoverRateAnalysisCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", "$stateParams", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService, $stateParams) {
            // tab标签展示布尔值
            var pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            // 分页信息
            $scope.pageInfo = angular.copy(pageInfo);
            // 导出功能信息
            $scope.exportParam = {
                url: ybHttp.RequestUrl("device", "/report/exportAgentAgentTurnoverRate"),
            };
            $scope.datePickerFlag = [false, false];
            // 搜索条件
            $scope.condition = {
                startDate:getCurrentMonthFirst(),
                endDate: new Date()
            };
            // 数据列表
            $scope.dataList = [];
            // 总计的数据
            $scope.totalRow = [];
            // 高级搜索和配置表格项的展示与隐藏
            $scope.toolbarConfig = {
                showAgentColConfig: false,
                showAgentAdSearch: false,
            };
            var TdWidth = 120;              // 表格列宽度
            var FirstThWidth = 200;         // 第一个表格头宽度
            // 公共表格头信息
            $scope.tbConf = {
                scrollWidth: FirstThWidth,
                searchTableCol: [],
                firstTableCol: [],
                secondTableCol: []
            };

            // 代理商表格头信息 拖拽要用到
            $scope.agentTbCol = [
                {
                    type: "",
                    name: "代理商名称",
                    dataField: "agentName",
                    width: FirstThWidth,
                    isShow: true,
                }
            ];

            // 高级搜索中的 选择分类信息
            $scope.selectClassInfo = {
                notCheckedClasses: [],
                selectClassesStr: '',
            };

            // 重置搜索条件
            $scope.reset = function () {
                $scope.condition = {};
                $scope.selectClassInfo = {
                    notCheckedClasses: [],
                    selectClassesStr: '',
                };
                setTableHead(true);
            };

            // 搜索周转率分析列表
            $scope.search = function (curPage) {
                getDataList(curPage);
            };

            // 获取当月第一天
            function getCurrentMonthFirst(){
                var date = new Date();
                date.setDate(1);
                return date;
            }

            // 切换高级搜索和表格配置展示与隐藏
            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig[toolbar]) {
                    $scope.toolbarConfig[toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig[toolbar] = true;
                    });
                }
            };

            // 检查日期
            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            // 切换时间展示 隐藏
            $scope.switchDataPicker = function (index) {
                $scope.datePickerFlag = [false, false];
                if (angular.isDefined(index)) {
                    $scope.datePickerFlag[index] = true;
                }
            };

            // 高级搜索中的选择分类
            $scope.selectClass = function () {
                var checkedClass = [];          // 已选中的分类
                var notCheckedClasses = [];
                angular.forEach($scope.tbConf.searchTableCol, function (item) {
                    if (item.isChecked) {
                        checkedClass.push(item.name);
                    } else {
                        notCheckedClasses.push(item.value);
                    }
                });
                $scope.selectClassInfo.selectClassesStr = checkedClass.join(',');
                $scope.selectClassInfo.notCheckedClasses = notCheckedClasses;
            };

            // 高级搜索中的搜索
            $scope.confirmSearch = function () {
                var notCheckLength = $scope.selectClassInfo.notCheckedClasses.length;
                var searchLength = $scope.tbConf.searchTableCol.length;
                if (notCheckLength === searchLength) {
                    ybWidget.ShowMessage("分类不能全部置为空！", MESG_TYPE.Warn);
                    return;
                }
                var classNameList = setTableHead(false);
                $scope.condition.classNameList = classNameList;
                $scope.search(1);
            };

            // 设置表格头信息
            function setTableHead (reset) {
                var classNameList = [];
                // 设置二级表格头的展示和隐藏
                var scrollWidth = $scope.agentTbCol[0].width;
                angular.forEach($scope.tbConf.secondTableCol, function (item) {
                    if ($scope.selectClassInfo.notCheckedClasses.indexOf(item.categoryType) >= 0 && !reset) {
                        item.isShow = false;
                    } else {
                        item.isShow = true;
                    }
                });
                // 设置一级表格头的展示和隐藏
                angular.forEach($scope.tbConf.firstTableCol, function (item) {
                    if ($scope.selectClassInfo.notCheckedClasses.indexOf(item.name) >= 0 && !reset) {
                        item.isShow = false;
                        classNameList.push(item.name);
                    } else {
                        item.isShow = true;
                        // 重新计算宽度
                        scrollWidth += item.width;
                    }
                });
                $scope.tbConf.scrollWidth = scrollWidth;
                return classNameList;
            }

            // 获取动态表格头
            function getTableHead () {
                var url = '/materialClass/getAllSecondlist';
                ybHttp.Get('device', url).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    var firstTableCol = [];           // 一级表格头
                    var secondTableCol = [];          // 二级表格头
                    var searchTableCol = [];          // 高级搜索中的分类搜索
                    angular.forEach(res.value, function (item) {
                        firstTableCol.push({
                            name: item.materialCalssName,
                            width: TdWidth*3,
                            isShow: true,
                        });
                        searchTableCol.push({
                            name: item.materialCalssName,
                            isChecked: true,
                            value: item.materialCalssName
                        });
                        secondTableCol.push({
                            name: "库存金额",
                            dataField: "inventoryAmount",
                            width: TdWidth,
                            isShow: true,
                            categoryType: item.materialCalssName || ''
                        });
                        secondTableCol.push({
                            name: "出库金额",
                            dataField: "outAmount",
                            width: TdWidth,
                            isShow: true,
                            categoryType: item.materialCalssName || ''
                        });
                        secondTableCol.push({
                            name: "周转率",
                            dataField: "turnoverRate",
                            width: TdWidth,
                            isShow: true,
                            categoryType: item.materialCalssName || ''
                        });
                    });
                    $scope.tbConf.searchTableCol = searchTableCol;
                    $scope.tbConf.scrollWidth = 200 + secondTableCol.length*TdWidth;
                    $scope.tbConf.firstTableCol = firstTableCol;
                    $scope.tbConf.secondTableCol = secondTableCol;
                })
            }

            // 获取代理商周转率报表列表
            function getDataList(curPage) {
                if (!$scope.condition.startDate ||!$scope.condition.endDate) {
                    ybWidget.ShowToaster('error', '请输入开始时间或结束时间');
                    return;
                }
                var reqUrl = "/report/agentAgentTurnoverRateList";
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                //遍历搜索条件对象里的空值并删除  不然搜索不出数据
                Object.keys($scope.condition).forEach(function (key) {
                    if (!$scope.condition[key] && $scope.condition[key] !== 0) delete $scope.condition[key];
                });
                var param = {
                    pageNum: $scope.pageInfo.curPage,
                    pageSize: $scope.pageInfo.pageSize,
                    filters: angular.copy($scope.condition)
                };
                ybHttp.Post('device', reqUrl, param).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    // $scope.totalRow = res.value.list.splice(0, 1);
                    $scope.pageInfo.totalRows = res.value.total;
                    $scope.pageInfo.totalPages = res.value.pages;
                    $scope.dataList = res.value.list;
                });
            }

            // 按钮权限code
            var btnPermissionCode = {
                agentTurnoverRateExport: window.ActionCode.agentTurnoverRateExport
            };

            // 按钮权限
            $scope.btnPermission = {
                agentTurnoverRateExport: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            (function () {
                UserContextService.SetBtnPermission(setBtnPermission);
                getTableHead();
            })();
        }])
    ;
})(window, window.angular);